<?php
/*
   Copyright 2011 BiSe Trojanov

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
 require_once('config.php');

?><!doctype html><html>
<head>
 <?php include('./include/header.inc'); ?>
 <title>SQL Manager / <?php echo $sad_domain; ?></title>

</head>
<body>
<?php include('./include/toolbar.inc');?>
<style>
abbr.byte0, abbr.byte1, abbr.byte2{
 color: black;
 cursor: pointer;
 border-bottom: 1px dashed;
}
abbr.byte1, abbr.byte2{display: none;}
</style>
<script>
 var type=0;
 function change_byte(){
  type++;if (type>2){type=0;}
  $('abbr.byte0').hide();$('abbr.byte1').hide();$('abbr.byte2').hide();
  $('abbr.byte'+type).show();
 }
</script>
<table><tr class="head">
 <td>Name</td><td>Engine</td><td>Rows</td><td><nobr>Data length</nobr></td>
 <td><nobr>Index length</nobr></td><td><span title="Autoincrement index">Index<span></td>
 <td><nobr>Collation</nobr></td><td><nobr>Update time</nobr></td></tr>
<?php
 if ($_GET['action']=='optimize'){
  $q=mysql_query('SHOW TABLE STATUS');
  while ($q_c=mysql_fetch_assoc($q)){
   if (strpos($q_c['Name'],$sad_prefix)===FALSE){continue;}
   mysql_query('optimize table `'.$q_c['Name'].'`');
  }
 }
 
 $q=mysql_query('SHOW TABLE STATUS');$len_data=0;$len_index=0;
 while ($q_c=mysql_fetch_assoc($q)){
  if (strpos($q_c['Name'],$sad_prefix)===FALSE){continue;}
  echo '<tr><td>'.$q_c['Name'].'</td><td>'.$q_c['Engine'].'</td><td>'.$q_c['Rows'].
       '</td><td><nobr>';
  echo '<abbr class="byte0" onclick="change_byte()">'.$q_c['Data_length'].'</abbr>';
  echo '<abbr class="byte1" onclick="change_byte()">'.drawsize($q_c['Data_length'],'1.2').'</abbr>';
  echo '<abbr class="byte2" onclick="change_byte()">'.drawsize($q_c['Data_length'],'2.2').'</abbr>';
  echo '</nobr></td><td><nobr>';
  echo '<abbr class="byte0" onclick="change_byte()">'.$q_c['Index_length'].'</abbr>';
  echo '<abbr class="byte1" onclick="change_byte()">'.drawsize($q_c['Index_length'],'1.2').'</abbr>';
  echo '<abbr class="byte2" onclick="change_byte()">'.drawsize($q_c['Index_length'],'2.2').'</abbr>';
  echo '</td><td><span title="Autoincrement index"><nobr>'.$q_c['Auto_increment'].
       '</nobr></span></td><td>'.$q_c['Collation'].'</td><td><nobr>'.$q_c['Update_time'].'</nobr></td></tr>';
  $len_data +=$q_c['Data_length'];
  $len_index+=$q_c['Index_length'];
 }
 echo '<td colspan="3">Sum</td><td>'.
      '<abbr class="byte0" onclick="change_byte()">'.$len_data.'</abbr>'.
      '<abbr class="byte1" onclick="change_byte()">'.drawsize($len_data,'1.2').'</abbr>'.
      '<abbr class="byte2" onclick="change_byte()">'.drawsize($len_data,'2.2').'</abbr>'.
      '</td><td>'.
      '<abbr class="byte0" onclick="change_byte()">'.$len_index.'</abbr>'.
      '<abbr class="byte1" onclick="change_byte()">'.drawsize($len_index,'1.2').'</abbr>'.
      '<abbr class="byte2" onclick="change_byte()">'.drawsize($len_index,'2.2').'</abbr>'.
      '</td></tr></table>';
 
 echo '<a href="?action=optimize" class="nicebutton mrgn">Optimize all tables</a>';
?>






</body></html>